#!/usr/bin/env python

import urllib
import re
import os

class URL(object):
	'''Make Webpage Resouce accessible and the same with local resource'''
	def __init__(self):
		print 'Init URL...'
		self.html = ''
		
	def __del__(self):
		print 'Deleting URL...'
		
	def LoadURL(self, url):
		print 'Entering URL::LoadURL()'
		httpRoot = 'http://garfield.library.upenn.edu/histcomp/' 
		# print url
		if url[0:4] == 'http' : # If it is a network address
			# print url
			op = urllib.urlopen(url)
			self.html = op.read()	
			# print self.html
		elif url[0:7] == './data/': #if it is a loca address
			try:
				op = open(url, "r")
				self.html = op.read()
				op.close()
			except: # if fail to open this file in local computer, downlod it from web
				url2 = httpRoot + url[7:]
				# print url2
				print 'Downloading file ' + url2 + '...'
				self.CheckDir(url)

				# For Debug
				# import pdb
				# pdb.set_trace()

				op = urllib.urlopen(url2)
				# print 'test'
				self.html = op.read()
				# print self.html
				# Save it to local disk
				# print url
							

				op2 = open(str(url), "w")
				op2.write(self.html)
				op2.close()
				print 'Saving the file to ' + url

		return self.html
				
				
	def CheckDir(self, dirAddr):
		print '[TEST] Entering CheckDir'
		# print dirAddr
		reobj = re.compile('/[a-zA-Z0-9_\-]+')
		pathName = re.findall(reobj, dirAddr)
		# print pathName
		folder = '.'
		for d in pathName[:(len(pathName)-1 )]:
			folder = folder + '/' + d[1:len(d)]
			# print folder
			if not os.path.exists(folder):
				os.mkdir(folder)
				print 'Creating a New Directory [' + folder + '] ...'	
	
